package gui;

import audio.WavFilter;
import javax.swing.UIManager;
import javax.swing.JFileChooser;
import java.io.File;
import javax.swing.JFrame;

/**
 * 
 * @author Peter Schilling, Jeff Hunter
 */
public class MainFrame extends JFrame {

	private int currentTabCount_;
	private int totalTabCount_;
	private byte[] copiedPoints;

	/** Creates new form GUI */
	public MainFrame() {
		currentTabCount_ = 1;
		totalTabCount_ = 1;
		initComponents();
	}
	
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        frameSizeGroup = new javax.swing.ButtonGroup();
        tabbedGraphPane = new javax.swing.JTabbedPane();
        menuBar1 = new javax.swing.JMenuBar();
        fileMenu1 = new javax.swing.JMenu();
        newMenuItem = new javax.swing.JMenuItem();
        openMenuItem1 = new javax.swing.JMenuItem();
        saveAsMenuItem1 = new javax.swing.JMenuItem();
        exitMenuItem1 = new javax.swing.JMenuItem();
        editMenu1 = new javax.swing.JMenu();
        cutMenuItem1 = new javax.swing.JMenuItem();
        copyMenuItem1 = new javax.swing.JMenuItem();
        pasteMenuItem1 = new javax.swing.JMenuItem();
        deleteMenuItem1 = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        filterSelectItem = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        filter32 = new javax.swing.JRadioButtonMenuItem();
        filter64 = new javax.swing.JRadioButtonMenuItem();
        filter128 = new javax.swing.JRadioButtonMenuItem();
        filter256 = new javax.swing.JRadioButtonMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        helpMenu1 = new javax.swing.JMenu();
        aboutMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(100, 100));
        setName("mainFrame"); // NOI18N
        getContentPane().setLayout(new java.awt.GridLayout(1, 0));

        tabbedGraphPane.setMinimumSize(new java.awt.Dimension(100, 100));
        tabbedGraphPane.setPreferredSize(new java.awt.Dimension(640, 400));

        tabbedGraphPane.add(new ViewPane(new File("tempFile" + totalTabCount_)), "Sample 1");

        getContentPane().add(tabbedGraphPane);
        tabbedGraphPane.getAccessibleContext().setAccessibleName("");

        fileMenu1.setText("File");
        fileMenu1.setFocusCycleRoot(true);
        fileMenu1.setFocusPainted(true);
        fileMenu1.setFocusTraversalPolicyProvider(true);

        newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newMenuItem.setText("New");
        newMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMenuItemActionPerformed(evt);
            }
        });
        fileMenu1.add(newMenuItem);

        openMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openMenuItem1.setText("Open");
        openMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuItem1ActionPerformed(evt);
            }
        });
        fileMenu1.add(openMenuItem1);

        saveAsMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveAsMenuItem1.setText("Save As ...");
        saveAsMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAsMenuItem1ActionPerformed(evt);
            }
        });
        fileMenu1.add(saveAsMenuItem1);

        exitMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        exitMenuItem1.setText("Exit");
        exitMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        fileMenu1.add(exitMenuItem1);

        menuBar1.add(fileMenu1);

        editMenu1.setText("Edit");

        cutMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        cutMenuItem1.setText("Cut");
        cutMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cutMenuItem1ActionPerformed(evt);
            }
        });
        editMenu1.add(cutMenuItem1);

        copyMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        copyMenuItem1.setText("Copy");
        copyMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                copyMenuItem1ActionPerformed(evt);
            }
        });
        editMenu1.add(copyMenuItem1);

        pasteMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
        pasteMenuItem1.setText("Paste");
        pasteMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pasteMenuItem1ActionPerformed(evt);
            }
        });
        editMenu1.add(pasteMenuItem1);

        deleteMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_DELETE, 0));
        deleteMenuItem1.setText("Delete");
        deleteMenuItem1.setDisplayedMnemonicIndex(0);
        deleteMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteMenuItem1ActionPerformed(evt);
            }
        });
        editMenu1.add(deleteMenuItem1);

        menuBar1.add(editMenu1);

        jMenu1.setText("Settings");

        filterSelectItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        filterSelectItem.setText("Filter Selection");
        filterSelectItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filterSelectItemActionPerformed(evt);
            }
        });
        jMenu1.add(filterSelectItem);

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.SHIFT_MASK));
        jMenuItem4.setText("Fourier Selection");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenu2.setText("Filter Size");

        frameSizeGroup.add(filter32);
        filter32.setText("32");
        filter32.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filter32ActionPerformed(evt);
            }
        });
        jMenu2.add(filter32);

        frameSizeGroup.add(filter64);
        filter64.setSelected(true);
        filter64.setText("64");
        filter64.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filter64ActionPerformed(evt);
            }
        });
        jMenu2.add(filter64);

        frameSizeGroup.add(filter128);
        filter128.setText("128");
        filter128.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filter128ActionPerformed(evt);
            }
        });
        jMenu2.add(filter128);

        frameSizeGroup.add(filter256);
        filter256.setText("256");
        filter256.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filter256ActionPerformed(evt);
            }
        });
        jMenu2.add(filter256);

        jMenu1.add(jMenu2);

        jMenu3.setText("Windowing Mode");

        jMenuItem1.setText("None");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuItem2.setText("Hamming");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        jMenuItem3.setText("Triangular");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem3);

        jMenu1.add(jMenu3);

        jMenu4.setText("Input Sample Rate");

        jMenuItem5.setText("11025");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem5);

        jMenuItem6.setText("22050");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem6);

        jMenuItem7.setText("44100");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem7);

        jMenu1.add(jMenu4);

        menuBar1.add(jMenu1);

        helpMenu1.setText("Help");

        aboutMenuItem1.setText("About");
        aboutMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutMenuItemActionPerformed(evt);
            }
        });
        helpMenu1.add(aboutMenuItem1);

        menuBar1.add(helpMenu1);

        setJMenuBar(menuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
		System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed

private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
	new AboutWindow();
}//GEN-LAST:event_aboutMenuItemActionPerformed

private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMenuItemActionPerformed
	tabbedGraphPane.add(new ViewPane(new File("tempFile" + ++totalTabCount_)), "Sample " + totalTabCount_);
	++currentTabCount_;
}//GEN-LAST:event_newMenuItemActionPerformed

private void openMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItem1ActionPerformed
	final JFileChooser fc = new JFileChooser();

	fc.setAcceptAllFileFilterUsed(false);
	fc.setFileFilter(new WavFilter());
	int returnVal = fc.showOpenDialog(this);

	File file = null;
	if (returnVal == JFileChooser.APPROVE_OPTION)
		file = fc.getSelectedFile();
	ViewPane vp = (ViewPane) tabbedGraphPane.getSelectedComponent();
	vp.loadFile(file);
}//GEN-LAST:event_openMenuItem1ActionPerformed

private void deleteMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteMenuItem1ActionPerformed
	ViewPane vp = (ViewPane) tabbedGraphPane.getSelectedComponent();
	vp.delete();
}//GEN-LAST:event_deleteMenuItem1ActionPerformed

private void cutMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cutMenuItem1ActionPerformed
	ViewPane vp = (ViewPane) tabbedGraphPane.getSelectedComponent();
	copiedPoints = vp.cut();
}//GEN-LAST:event_cutMenuItem1ActionPerformed

private void copyMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyMenuItem1ActionPerformed
	ViewPane vp = (ViewPane) tabbedGraphPane.getSelectedComponent();
	copiedPoints = vp.copy();
}//GEN-LAST:event_copyMenuItem1ActionPerformed

private void pasteMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pasteMenuItem1ActionPerformed
	ViewPane vp = (ViewPane) tabbedGraphPane.getSelectedComponent();
	if (copiedPoints != null)
		vp.paste(copiedPoints);
}//GEN-LAST:event_pasteMenuItem1ActionPerformed

private void filter64ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filter64ActionPerformed
	((ViewPane) tabbedGraphPane.getSelectedComponent()).setNumBins(64);
}//GEN-LAST:event_filter64ActionPerformed

private void filter32ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filter32ActionPerformed
	((ViewPane) tabbedGraphPane.getSelectedComponent()).setNumBins(32);
}//GEN-LAST:event_filter32ActionPerformed

private void filter128ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filter128ActionPerformed
	((ViewPane) tabbedGraphPane.getSelectedComponent()).setNumBins(128);
}//GEN-LAST:event_filter128ActionPerformed

private void filter256ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filter256ActionPerformed
	((ViewPane) tabbedGraphPane.getSelectedComponent()).setNumBins(256);
}//GEN-LAST:event_filter256ActionPerformed

private void filterSelectItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filterSelectItemActionPerformed
	((ViewPane) tabbedGraphPane.getSelectedComponent()).filterSelection();
}//GEN-LAST:event_filterSelectItemActionPerformed

private void saveAsMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsMenuItem1ActionPerformed
	final JFileChooser fc = new JFileChooser();

	fc.setAcceptAllFileFilterUsed(false);
	fc.setFileFilter(new WavFilter());
	int returnVal = fc.showSaveDialog(this);

	File file = null;
	if (returnVal == JFileChooser.APPROVE_OPTION)
		file = fc.getSelectedFile();
	ViewPane vp = (ViewPane) tabbedGraphPane.getSelectedComponent();

	vp.saveFile(file);//GEN-LAST:event_saveAsMenuItem1ActionPerformed
	}

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
	((ViewPane) tabbedGraphPane.getSelectedComponent()).setWindowMode(0);
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
	((ViewPane) tabbedGraphPane.getSelectedComponent()).setWindowMode(1);
}//GEN-LAST:event_jMenuItem2ActionPerformed

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
	((ViewPane) tabbedGraphPane.getSelectedComponent()).setWindowMode(2);
}//GEN-LAST:event_jMenuItem3ActionPerformed

private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
	((ViewPane) tabbedGraphPane.getSelectedComponent()).fourier();
}//GEN-LAST:event_jMenuItem4ActionPerformed

private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
	((ViewPane) tabbedGraphPane.getSelectedComponent()).setSampleRate(11025f);
}//GEN-LAST:event_jMenuItem5ActionPerformed

private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
	((ViewPane) tabbedGraphPane.getSelectedComponent()).setSampleRate(22050f);
}//GEN-LAST:event_jMenuItem6ActionPerformed

private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
	((ViewPane) tabbedGraphPane.getSelectedComponent()).setSampleRate(44100f);
}//GEN-LAST:event_jMenuItem7ActionPerformed


public void removeTab(ViewPane vp){
	tabbedGraphPane.remove(vp);
	currentTabCount_--;
	if(currentTabCount_ == 0)
		System.exit(0);
}

    /**
     * @param args the command line arguments
     */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					UIManager.put("FileChooser.readOnly", Boolean.TRUE);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				MainFrame audioAnalyzer = new MainFrame();
				audioAnalyzer.setExtendedState(audioAnalyzer.getExtendedState()|JFrame.MAXIMIZED_BOTH);
				audioAnalyzer.setVisible(true);
			}
		});
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem aboutMenuItem1;
    private javax.swing.JMenuItem copyMenuItem1;
    private javax.swing.JMenuItem cutMenuItem1;
    private javax.swing.JMenuItem deleteMenuItem1;
    private javax.swing.JMenu editMenu1;
    private javax.swing.JMenuItem exitMenuItem1;
    private javax.swing.JMenu fileMenu1;
    private javax.swing.JRadioButtonMenuItem filter128;
    private javax.swing.JRadioButtonMenuItem filter256;
    private javax.swing.JRadioButtonMenuItem filter32;
    private javax.swing.JRadioButtonMenuItem filter64;
    private javax.swing.JMenuItem filterSelectItem;
    private javax.swing.ButtonGroup frameSizeGroup;
    private javax.swing.JMenu helpMenu1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuBar menuBar1;
    private javax.swing.JMenuItem newMenuItem;
    private javax.swing.JMenuItem openMenuItem1;
    private javax.swing.JMenuItem pasteMenuItem1;
    private javax.swing.JMenuItem saveAsMenuItem1;
    private javax.swing.JTabbedPane tabbedGraphPane;
    // End of variables declaration//GEN-END:variables
}
